package get

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
)

func Insert_record(r *gin.Engine) {
	var err error
	var db *sql.DB
	db, err = sql.Open("postgres", "port=5432 user=postgres password=system dbname=databasedesign sslmode=disable")

	r.GET("/insertlog", func(c *gin.Context) {
		// 获取参数
		sid := c.Query("sid")
		date := c.Query("date")
		mal := c.Query("mal")
		var code string
		code = "0"

		if err != nil {
			panic(err)
		}

		stmt, err := db.Prepare("INSERT INTO test(sid,mal,date) VALUES($1,$2,$3)")
		if err != nil {
			panic(err)
		}

		res, err := stmt.Exec(sid, mal, date)
		if err != nil {
			panic(err)
		}

		affect, err := res.RowsAffected()
		if err != nil {
			panic(err)
		}

		fmt.Println("rows affect:", affect)

		code = "10"

		c.JSON(200, gin.H{
			"sid":  sid,
			"mal":  mal,
			"date": date,
			"code": code,
		})

	})
}
